package cc.rengu.igas.cops.core.service.trans.asyncall;

/*本行卡实时清算异步处理*/

import cc.rengu.igas.cops.common.constant.CopsTreeNodeConstant;
import cc.rengu.igas.cops.common.enums.RespCodeEnum;
import cc.rengu.oltp.service.base.DubboAsynBaseService;
import cc.rengu.oltp.service.common.dao.UserOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.UserOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.OrderService;
import cc.rengu.oltp.service.realize.impl.OrderServiceImpl;
import cc.rengu.oltp.utility.annotation.BIZ_PROCESS;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

import java.util.ArrayList;
import java.util.List;

import static cc.rengu.oltp.service.common.enums.OltpRpcdEnum.BIZ_PROCESS_TIMEOUT;
import static cc.rengu.oltp.service.common.enums.OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND;

public class CopsDealAsynRealTimeClearsService extends DubboAsynBaseService {
    @BIZ_PROCESS
    public void dealAsynPosRealTimeClearsService() throws Exception {
        rglog.debug("~~~~~~~~~~~~~本行实时清算开始~~~~~~~~~~~~~~~~~~~~");
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        OrderService orderService = new OrderServiceImpl();
        /*获取商户订单表*/
        MchntOrderInfo mchntOrderInfo = (MchntOrderInfo) xmlTreeUtil.getXmlTreeObjectValue(CopsTreeNodeConstant.MCHNT_ORDER_INFO, MchntOrderInfo.class);
        if (null == mchntOrderInfo) {
            rglog.debug("获取商户订单表异常");
            throw new BizException(OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespCode(), OltpRpcdEnum.RECOND_EMPTY_OR_NOT_FOUND.getRespDesc());
        }
        List<MchntOrderInfo> list = new ArrayList<>();

        //TODO 获取调用结果
        String respCode = "";
        String respDesc = "";
        String sysSeqNum = "";
        /*获取本行实时清算支付订单表*/
        PayOrderInfo realTimeClearPayOrderInfo = (PayOrderInfo) xmlTreeUtil.getXmlTreeObjectValue(CopsTreeNodeConstant.REAL_TIME_CLEAR_ORDER_INFO, PayOrderInfo.class);
        if (null == realTimeClearPayOrderInfo) {
            rglog.error("获取本行实时清算支付订单表异常");
            throw new BizException(RECOND_EMPTY_OR_NOT_FOUND.getRespCode(), RECOND_EMPTY_OR_NOT_FOUND.getRespDesc());
        }
        realTimeClearPayOrderInfo.setRespDesc(respDesc);
        realTimeClearPayOrderInfo.setRespCode(respCode);
        //realTimeClearPayOrderInfo.setTransBatchNo(response.getChnlPayInfo().getTransBatchNo());
        /*调用本行实时清算 成功处理*/
        if (RespCodeEnum.TRANS_SUCCESS.getRespCode().equals(respCode)) {
            /*设置本行实时清算支付订单的状态*/
//            realTimeClearPayOrderInfo.setSettleDate(response.getChnlPayInfo().getSettleDate());
//            realTimeClearPayOrderInfo.setChannelSeqNum(response.getChnlPayInfo().getChannelSeqNum());
            realTimeClearPayOrderInfo.setTransStatus("1");
            realTimeClearPayOrderInfo.setBizStatus("00");
            /*更改商户订单表实时清算状态*/
            mchntOrderInfo.setRealSettleStatus("00");
            list.add(mchntOrderInfo);
            /*更新本行实时清算支付订单表*/
            orderService.updateOrderInfo(null, list, realTimeClearPayOrderInfo);
            /*调用消息推送*/
            /*记账成功后，调用微信消息推送*/
            /*获取用户订单*/
            UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
            UserOrderInfo userOrderInfo = userOrderInfoMapper.selectUserOrderInfoByPrimaryKey(mchntOrderInfo.getInstId(), mchntOrderInfo.getSysSeqNum());
            if (null == userOrderInfo) {
                rglog.debug("获取用户订单表异常");
                throw new BizException(RECOND_EMPTY_OR_NOT_FOUND.getRespCode(), RECOND_EMPTY_OR_NOT_FOUND.getRespDesc());
            }
            xmlTreeUtil.setXmlTreeObjectValue("SHARE_ORIG_USER_ORDER_INFO", userOrderInfo);
            int iReturnCode = asynnotify.transactionNotice("MchntTransNoticeService", 1, 0, 0);
            if (0 != iReturnCode) {
                rglog.error("提交交易结果异步通知给商户失败!");
            }
        } else if (BIZ_PROCESS_TIMEOUT.getRespCode().equals(respCode)) {
            /*失败更新状态*/
            /*设置本行实时清算支付订单的状态*/
            realTimeClearPayOrderInfo.setTransStatus("3");
            /*更改商户订单表实时清算状态*/
            mchntOrderInfo.setRealSettleStatus("02");
            list.add(mchntOrderInfo);
            /*更新本行实时清算支付订单表*/
            orderService.updateOrderInfo(null, list, realTimeClearPayOrderInfo);
        } else {
            /*设置本行实时清算支付订单的状态*/
            realTimeClearPayOrderInfo.setTransStatus("2");
            realTimeClearPayOrderInfo.setBizStatus("02");
            /*更改商户订单表实时清算状态*/
            mchntOrderInfo.setRealSettleStatus("01");
            list.add(mchntOrderInfo);
            /*更新本行实时清算支付订单表*/
            orderService.updateOrderInfo(null, list, realTimeClearPayOrderInfo);
        }
        rglog.debug("~~~~~~~~~~~~~本行实时清算结束~~~~~~~~~~~~~~~~~~~~");
    }
}

